#include <map>
#include <iostream>
using namespace std;


void test1()   //遍历
{
	/*map<string, string> dict;
	pair<string, string> kv1("left", "左边");*/
	
	////map的多种初始化方式
	//dict.insert(kv1);
	//dict.insert(pair<string, string>("right", "右边"));
	//dict.insert(make_pair("insert", "插入"));

	////pair<string, string> kv2 = {"string","字符串" };
	//dict.insert({ "string", "字符串" });

	map<string, string> dict = { {"left", "左边"}, {"right", "右边"},{"insert", "插入"},{ "string", "字符串" } };

	map<string, string>::iterator it = dict.begin();
	while (it != dict.end())
	{
		//cout << (*it).first <<":"<<(*it).second << endl;
		//cout << (*it).first << ":" << (*it).second << endl;
		cout << it->first << ":" << it->second << endl;

		++it;
	}
	cout << endl;
	for (const auto& e : dict)
	{
		cout << e.first << ":" << e.second << endl;
	}
	cout << endl;

	for (auto& [x, y] : dict) //C++17支持
	{
		cout << x << ":" << y << endl;
	}
	cout << endl;

	string str;
	while (cin >> str)
	{
		auto ret = dict.find(str);
		if (ret != dict.end())
		{
			cout << "->" << ret->second << endl;
		}
		else
		{
			cout << "无此单词，请重新输入" << endl;
		}
	}
}


void test2()  //映射计数
{
	string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉" };
	int i = int(); //匿名构造, 为0
	int j = int(10);
	map<string, int> countTree; //故其second的值最初为0
	for (const auto& str : arr)
	{
		// 先查找水果在不在搜索树中
		// 1、不在，说明水果第一次出现，则插入<水果, 1>
		// 2、在，则查找到的节点中水果对应的次数++
		//BSTreeNode<string, int>* ret = countTree.Find(str);
		auto ret = countTree.find(str);  
		if (ret == countTree.end())  //没有找到
		{
			countTree.insert({ str, 1 });
		}
		else
		{
			ret->second++;
		}
	}
	
	for (const auto& e : countTree)
	{
		cout << e.first << ":" << e.second << endl;
	}
	cout << endl;


	map<string, int> countTree1; //第二种统计方法
	for (const auto& str : arr)
	{
		countTree1[str]++;
	}
	for (const auto& e : countTree1)
	{
		cout << e.first << ":" << e.second << endl;
	}
	cout << endl;
}



void test3()   //插入
{
	map<string, string> dict;
	dict.insert(make_pair("sort", "排序"));
	//插入 + 修改
	dict["left"] = "左边"; 

	//修改
	dict["left"] = "左边 + 修改";

	//key不存在->插入<"insert", "">
	dict["insert"];

	//存在->查找
	cout << dict["left"] << endl;

}


int main()
{
	//test1();
	
	test3();

	return 0;
}